package com.H1023.controller;

import com.H1023.pojo.Book;
import com.H1023.service.BookService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
public class QueryBookController {
    @Autowired
    @Qualifier("bookServiceImpl")
    private BookService bookService;


    @RequestMapping("/queryBook")
    public String toQueryBookController(String detail, String select, Model model) {


        model.addAttribute("detail",detail);
        model.addAttribute("select",select);

        if(detail == ""&&"全部".equals(select))
            model.addAttribute("bookList", JSON.toJSONString(bookService.queryAll()));
        else if(detail!=""&&"全部".equals(select)){
            boolean flag=true;
            String bookList="";
            for(int i = detail.length();--i>=0;){
               if(!Character.isDigit(detail.charAt(i)));
                    flag=false;
            }
            if(flag){
                bookList += JSON.toJSONString(bookService.queryByIsbn(detail));
            }
            if(bookList == "") {
                bookList += JSON.toJSONString(bookService.queryByDetailAndAll(detail));
            }
            model.addAttribute("bookList",bookList);
        }
        else if("ISBN".equals(select))
                model.addAttribute("bookList", JSON.toJSONString(bookService.queryByIsbn(detail)));

        else if("书名".equals(select))
            model.addAttribute("bookList", JSON.toJSONString(bookService.queryByName(detail)));

        else if("作者".equals(select))
            model.addAttribute("bookList", JSON.toJSONString(bookService.queryByAuthor(detail)));

        else if("出版社".equals(select))
            model.addAttribute("bookList", JSON.toJSONString(bookService.queryByPress(detail)));

        else if("主要内容".equals(select))
            model.addAttribute("bookList", JSON.toJSONString(bookService.queryByDetail(detail)));

        else
            model.addAttribute("bookList", JSON.toJSONString(bookService.queryByType(detail)));


        return "search";


    }


}
